package imse.team0.scalability;

import org.apache.camel.Exchange;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Evgenia
 * Date: 5/22/12
 * Time: 5:44 PM
 * To change this template use File | Settings | File Templates.
 */
public class LoadBalancer {
    private volatile int endpointIndex = 0;
    private List<String> endpointAddresses = new ArrayList<String>();

    public LoadBalancer() {
        endpointAddresses.add("192.168.2.2:8182");
        // endpointAddresses.add("192.168.2.3:8182");
    }

    public String getWSEndpointAddress(Exchange exchange) {
        if (exchange.getProperty(Exchange.SLIP_ENDPOINT) != null) {
            //exchange.getIn().setHeader("SOAP_REPLY", exchange.getIn().getBody());
            return null;
        }
        String endpoint = "";
        int index = roundRobinStrategy();
        return "cxf://http://" + endpointAddresses.get(index) + "/cxf/order?serviceClass=imse.team0.orderws.OrderEndpoint&defaultOperationName=placeOrder";
    }

    private int roundRobinStrategy() {
        int index = endpointIndex;
        endpointIndex = (endpointIndex+1) % endpointAddresses.size();
        return index;
    }
}
